/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.task.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.Task.TaskFlowItem;
import com.naizhuai.ui.entity.mainbusiness.ActionShowMainItemBean;
import com.naizhuai.ui.task.StepJoinActivity;
import com.naizhuai.ui.task.StepSettingActivity;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.MathUtils;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.ViewUtils;

import java.util.Collections;
import java.util.List;

public class TaskFlowAdapter extends RecyclerView.Adapter<TaskFlowAdapter.ActionItemDetailsHolder> {

    private List<TaskFlowItem> actionList= Collections.emptyList();
    private Context mContext;
    public TaskFlowAdapter(Context context) {
        this.mContext = context;
    }

    public void setActionList(List<TaskFlowItem> actionList) {
        this.actionList = actionList;
    }

    @Override
    public int getItemCount() {
        return actionList.size();
    }

    @NonNull
    @Override
    public ActionItemDetailsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_flow_item, parent, false);
        return new ActionItemDetailsHolder(sView);
    }


    @Override
    public void onBindViewHolder(ActionItemDetailsHolder imageHolder, int position) {
       final TaskFlowItem actionItemBean = actionList.get(position);
        ViewUtils.showRoundImg(actionItemBean.thumb,imageHolder.chargeThumb);
        ViewUtils.showImg(actionItemBean.flowImage,imageHolder.imageContent);
        imageHolder.chargeName.setText(actionItemBean.username);
        imageHolder.actionStatus.setText(actionItemBean.statusName);
        imageHolder.actionName.setText(actionItemBean.flowName);
        double proRadius = ActionShowMainItemBean.getProcessStatus(actionItemBean.status);
        double radiusUp = MathUtils.numberSubstrationReNUmber(System.currentTimeMillis() + "", actionItemBean.flowStartTime);
        double radiusDown = MathUtils.numberSubstrationReNUmber(actionItemBean.flowEndTime, actionItemBean.flowStartTime);
        double timeRadius;
        if (radiusDown <=0){
             timeRadius = 1.0;
        }else {
             timeRadius = radiusUp / radiusDown;
        }

        imageHolder.itemView.post(() -> {
            int totalWidth = imageHolder.processTotal.getWidth();
            int proTotalWidth = (int) (totalWidth * proRadius);
            int timeTotalWidth = (int) (totalWidth * timeRadius);
            NaizhuAILog.d(timeTotalWidth+"");
            RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) imageHolder.actionProcess.getLayoutParams();
            params.width=proTotalWidth;
            imageHolder.actionProcess.setLayoutParams(params);

            RelativeLayout.LayoutParams params1= (RelativeLayout.LayoutParams) imageHolder.timeProcess.getLayoutParams();
            params1.width=timeTotalWidth;
            imageHolder.timeProcess.setLayoutParams(params1);
         });
        imageHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  switch (actionItemBean.status){
                      case TaskFlowItem.DOING:
                      case TaskFlowItem.COMPLETED:
                           StepJoinActivity.start(mContext,actionItemBean.flowId,actionItemBean.step);
                          break;
                      case TaskFlowItem.PREPARE:
                          StepSettingActivity.start(mContext,actionItemBean.flowId);
                          break;
                  }
            }
        });
    }

    public class ActionItemDetailsHolder extends RecyclerView.ViewHolder {
        CircleImageView chargeThumb;
        TextView chargeName, actionStatus, actionName;
        ImageView imageContent;
        View processTotal, actionProcess, timeProcess;
        public ActionItemDetailsHolder(View view) {
            super(view);
            chargeThumb = view.findViewById(R.id.action_charge_thumb);
            chargeName = view.findViewById(R.id.action_charge_user_name);
            actionStatus = view.findViewById(R.id.action_status);
            actionName = view.findViewById(R.id.action_name);
            imageContent = view.findViewById(R.id.action_item_image_content);
            processTotal = view.findViewById(R.id.total_process_line);
            actionProcess = view.findViewById(R.id.process_line);
            timeProcess = view.findViewById(R.id.time_line);
        }

    }




}
